<?php
App::import('Vendor', 'smtp');
//App::import('Vendor', 'ClassPhpmailer', array('file' => 'phpmailer'.DS.'class.phpmailer.php'));
class UsersController extends AppController {

	var $name = 'Users';
	
	function beforeFilter() {
    parent::BeforeFilter();   	
	$this->Auth->allow('login','logout','userlist','add','confirmaccount','testmail');
	//$this->Auth->allow('*');
}

function home() {
           
       
    }

    function accept($id) {

$this->User->id=$id;
$this->User->saveField('accept', '1');
$this->Session->setFlash(__('Successfully Accepted.', true));
$this->redirect(array('controller' => 'users','action' => 'index'));
    }

    function reject($id) {
$this->User->id=$id;

$this->User->saveField('accept', '0');
$this->Session->setFlash(__('Successfully Rejected.', true));
$this->redirect(array('controller' => 'users','action' => 'index'));
    }
	
	function common() {
		if($user = $this->Session->read('Auth.User')) {
			if($user['role'] == 1) {
				$this->redirect(array('controller' => 'users','action' => 'index'));	
			}else{
			$this->redirect(array('controller' => 'users','action' => 'home'));
			}
			
		}
	}

function login() {
           
       
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }


	function index() {
		$this->User->recursive = 0;
                $this->paginate = array(
        'conditions' => array('User.role !=' => 1),
        'limit' => 30
    );
		$this->set('users', $this->paginate());
	}

        function gradation() {
            $this->layout='';
		$this->User->recursive = 0;
                $this->paginate = array(
        'conditions' => array('User.role !=' => 1),
        'limit' => 3000,
        'order'=>array('Uinformation.gradationnumber'=>'asc')
    );
//    pr($this->paginate('User'));
//    exit;

		$this->set('users', $this->paginate());
	}


        function sindex() {
	if (!empty($this->data)) {
            $this->User->recursive = 0;
                $this->paginate = array(
        'conditions' => array('User.fullname LIKE' =>'%'.$this->data['User']['fullname'].'%')
        
    );
		$this->set('users', $this->paginate());
        }else{
          $this->set('users', '1');
	}
        }

               
        function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
                    $to = $this->data['User']['username'];
			$this->User->create();
			if ($this->User->save($this->data)) {
                        $userid=$this->User->getLastInsertId();
                        $smtp = new SMTP('ssl://smtp.gmail.com',465, false, 5);
                        $smtp->auth('kodegeek10@gmail.com', '123456aa');
                        # Who is this email from? (MAIL FROM header)
                        $smtp->mail_from('pmis.admin@gmail.com');
                        //$to='bahar.iiuc@gmail.com';
                        # Sending the email to you@example.com
                         $subject = "Please Activate Your PMIS Account.";
                            $message = "Thank you for your registration on PMIS. Please click the following link to activate your account: http://sitesdisplay.co.cc/users/confirmaccount/$userid"."\n\n"."Thank's"."\n"."Admin";
                        $smtp->send($to, $subject, $message);
			$this->Session->setFlash(__('Your registration are successfully completed.Please check your mail for activated your account!!!', true));
			$this->redirect(array('controller' => 'users','action' => 'login'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
		////////////////confrim account/////////
function confirmaccount($userid){
			$this->User->id=$userid;	
			$this->data['User']['act']=1;					
					
		$isexst=$this->User->findById("$userid");			
		if(!empty($isexst)){
		if ($this->User->save($this->data)) 
			{
			$this->Session->setFlash(__('Your account are successfully activated.', true));
			$this->redirect('/'); 

			}else{
			$this->Session->setFlash(__('Invalid Operation.Please, try again.', true));
			$this->redirect('/'); 

			}	

			}else{
			$this->Session->setFlash(__('Invalid Operation.Please, try again.', true));
			$this->redirect('/'); 
			}	

		}

                function testmail(){
                    $this->layout=FALSE;
                    $this->autoRender=FALSE;
                   
$mail = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
//$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "ssl://smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "kodegeek10@gmail.com";  // GMAIL username
$mail->Password   = "123456aa";            // GMAIL password

$mail->SetFrom('name@yourdomain.com');

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->Body    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->AddAddress('bahar.iiuc@gmail.com');
if(!$mail->Send())
{
   echo "Message was not sent<br/ >";
   echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
   echo "Message has been sent";
}

                }
               
}
?>